//
//  ZBLEDataModel.m
//  ZRGP
//
//  Created by Jun on 2020/10/9.
//  Copyright © 2020 HWD. All rights reserved.
//

#import "ZBLEDataModel.h"
#import "JCDataConvert.h"

@implementation ZBLEDataModel

- (instancetype)initWith:(NSData *)data{
    
    self = [super init];
    if (self) {
        self.ackNum = [JCDataConvert oneByteToDecimalUint:[data subdataWithRange:NSMakeRange(2, 1)]];
        if (self.ackNum == 1&&data.length>18) {
            //数据包一
            //电池组电压
            self.bVoltage = [JCDataConvert twoBytesToDecimalUint:[data subdataWithRange:NSMakeRange(4, 2)]];
            //电池电流
//            self.bCurrent = [JCDataConvert twoBytesToDecimalUint:[data subdataWithRange:NSMakeRange(6, 2)]];
            NSString *data_str = [JCDataConvert convertDataToHexStr:[data subdataWithRange:NSMakeRange(6, 2)]];
            self.bCurrent = [self input0x16String:data_str];
            //电池温度
            self.bTemp = [JCDataConvert twoBytesToDecimalUint:[data subdataWithRange:NSMakeRange(8, 2)]];
            //SOC
            self.bSoc = [JCDataConvert oneByteToDecimalUint:[data subdataWithRange:NSMakeRange(10, 1)]];
            //SOH
            self.bSoh = [JCDataConvert oneByteToDecimalUint:[data subdataWithRange:NSMakeRange(11, 1)]];
            //循环次数
            self.bCycleNum = [JCDataConvert twoBytesToDecimalUint:[data subdataWithRange:NSMakeRange(12, 2)]];
            //剩余容量
            self.bDumpVolume = [JCDataConvert twoBytesToDecimalUint:[data subdataWithRange:NSMakeRange(14, 2)]];
            //充满容量
            self.bFullVolume = [JCDataConvert twoBytesToDecimalUint:[data subdataWithRange:NSMakeRange(16, 2)]];
            //self.bFullVolume = 6666;
        
        }
        
        if (self.ackNum == 2&&data.length>18) {
            //数据包二
            //放空时间
            self.bEmptyTime = [JCDataConvert twoBytesToDecimalUint:[data subdataWithRange:NSMakeRange(4, 2)]];
            //充满时间
            self.bFullTime = [JCDataConvert twoBytesToDecimalUint:[data subdataWithRange:NSMakeRange(6, 2)]];
            //电池温度
            //self.bTemp2 = [JCDataConvert oneByteToDecimalUint:[data subdataWithRange:NSMakeRange(8, 2)]];
            //单体电压1
            self.cellVoltage1 = [JCDataConvert twoBytesToDecimalUint:[data subdataWithRange:NSMakeRange(8, 2)]];
            //单体电压2
            self.cellVoltage2 = [JCDataConvert twoBytesToDecimalUint:[data subdataWithRange:NSMakeRange(10, 2)]];
            //单体电压3
            self.cellVoltage3 = [JCDataConvert twoBytesToDecimalUint:[data subdataWithRange:NSMakeRange(12, 2)]];
            //单体电压4
            self.cellVoltage4 = [JCDataConvert twoBytesToDecimalUint:[data subdataWithRange:NSMakeRange(14, 2)]];
            //电池状态
            //self.bState = [JCDataConvert twoBytesToDecimalUint:[data subdataWithRange:NSMakeRange(16, 2)]];
            Byte byte[data.length];
            [data getBytes:byte length:data.length];
            Byte array1[16] = {0};
            for (int i = 15; i >= 0; i--) {
                array1[i] = (Byte)((((byte[16]<<8)|byte[17])>>(15-i))&1);
                //NSLog(@"array1 %d = %hhu",i,array1[i]);
            }
            
            if (array1[11] == 1) {
                //放空--
                self.bState = bEmptyState;
            }
            if (array1[10] == 1) {
                //充满--
                self.bState = bFullState;
            }
            if (array1[9] == 1) {
                //放电静置（静置）
                self.bState = bSilenceState;
            }
            
            self.bState_01 = array1[15];
            self.bState_02 = array1[14];
            self.bState_03 = array1[13];
            self.bState_04 = array1[12];
            self.bState_05 = array1[11];
            self.bState_06 = array1[10];
            self.bState_07 = array1[9];
            self.bState_08 = array1[8];
            self.bState_09 = array1[7];
            self.bState_10 = array1[6];
            self.bState_11 = array1[5];
            self.bState_12 = array1[4];
            self.bState_13 = array1[3];
            self.bState_14 = array1[2];
            self.bState_15 = array1[1];
            self.bState_16 = array1[0];
        }
        
        
    }
    return self;
}


- (int)input0x16String:(NSString *)string{
    char *_0x16String = (char *)string.UTF8String;
    NSMutableString *binaryString = [[NSMutableString alloc] init];
    for (int i = 0; i < string.length; i++) {
        char c = _0x16String[i];
        NSString *binary = [self binaryWithHexadecimal:[NSString stringWithFormat:@"%c",c]];
        [binaryString appendString:binary];
    }
    
    if ([binaryString characterAtIndex:0] == '1') {
        //反码
        for (int i = (int)binaryString.length - 1; i > 0; i--) {
            char c = [binaryString characterAtIndex:i];
            c = c^0x1;
            [binaryString replaceCharactersInRange:NSMakeRange(i, 1) withString:[NSString stringWithFormat:@"%c",c]];
        }
        
        //补码
        BOOL flag = NO; //进位
        NSInteger lastIndex = binaryString.length - 1;
        char lastChar = [binaryString characterAtIndex:lastIndex];
        if (lastChar == '0') {
            lastChar = '1';
        } else {
            lastChar = '0';
            flag = YES;
        }
        
        [binaryString replaceCharactersInRange:NSMakeRange(lastIndex, 1) withString:[NSString stringWithFormat:@"%c",lastChar]];
        
        if (flag) {
            for (int i = (int)binaryString.length - 2; i > 0; i--) {
                char c = [binaryString characterAtIndex:i];
                if (flag) {//进位
                    if (c == '0') {
                        c = '1';
                        flag = NO;
                        [binaryString replaceCharactersInRange:NSMakeRange(i, 1) withString:[NSString stringWithFormat:@"%c",c]];
                        break;
                    } else if (c == '1'){
                        c = '0';
                        flag = YES;
                        [binaryString replaceCharactersInRange:NSMakeRange(i, 1) withString:[NSString stringWithFormat:@"%c",c]];
                    }
                }
            }
        }
    }
    
    int result = 0;
    int bit = 0;
    //计算
    for (int i = (int)binaryString.length - 1; i > 0; i--) {
        char c = [binaryString characterAtIndex:i];
        if (c == '1') {
            result += pow(2, bit);
        }
        ++bit;
    }
    if ([binaryString characterAtIndex:0] == '1') {
        result = result *-1;
    }
    return result;
}

- (NSString *)binaryWithHexadecimal:(NSString *)string{
    // 现将16进制转换车无符号的10进制
    long a = strtoul(string.UTF8String, NULL, 16);
    NSMutableString *binary = [[NSMutableString alloc] init];
    while (a/2 !=0) {
        [binary insertString:[NSString stringWithFormat:@"%ld",a%2] atIndex:0];
        a = a/2;
    }
    
    [binary insertString:[NSString stringWithFormat:@"%ld",a%2] atIndex:0];
    
    //不够4位的高位补0
    while (binary.length%4 !=0) {
        [binary insertString:@"0" atIndex:0];
    }
    return binary;
}


@end
